<template>
  <div class="siteSkill">
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xxl="5" :xl="6">
            <p-form-item label="技能编码">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.skillCode"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <p-form-item label="技能名称">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.skillName"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <p-form-item label="技能描述">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.skillSprc"></p-input>
            </p-form-item>
          </p-col>

          <p-col :xxl="5" :xl="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuerySelf" icon="search">查询</p-button>
              <p-button type="primary" @click="searchResetSelf" icon="reload" style="margin-left: 8px">重置</p-button>
            </span>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <p-button @click="handleAddShkill" type="primary" icon="plus">新增</p-button>
       <!-- 高级查询区域 -->
       <!-- <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> -->
       <p-button :disabled="!selectedRowKeys.length > 0" @click="batchDelSkill" style="margin-left: 8px">
          <p-icon type="delete" />删除 
       </p-button>
      <!--  <p-dropdown v-if="selectedRowKeys.length > 0">
        <p-menu slot="overlay">
          <p-menu-item key="1" @click="batchDel"><p-icon type="delete"/>删除</p-menu-item>
        </p-menu>
        <p-button style="margin-left: 8px"> 批量操作 <p-icon type="down" /></p-button>
      </p-dropdown> -->
    </div>
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>

      <span style="float:right;">
        <!-- <a @click="loadData()"><p-icon type="sync" />刷新</a> -->
        <p-divider type="vertical" />
          <p-popover title="自定义列" trigger="click" placement="leftBottom"  :overlay-style="getWidth">
          <template slot="content">
            <p-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <p-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <p-col :span="12"><p-checkbox :value="item.dataIndex">{{ item.title }}</p-checkbox></p-col>
                  </template>
                </template>
              </p-row>
            </p-checkbox-group>
          </template>
          <a><p-icon type="setting" />自定义列</a>
        </p-popover>
      </span>
    </div>
    <p-table
      ref="table"
      size="middle"
      bordered
      rowKey="skillCode"
      :columns="columns"
      :dataSource="options"
      :pagination="false"
      :scroll="{ y: 390 }"
      :loading="loading"
      :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
        <span slot="action" slot-scope="text, record, index">
          <p-popconfirm title="确定删除吗?" @confirm="() => handleDeleteSkill(index)">
            <a>删除</a>
          </p-popconfirm>
        </span>
    </p-table>
    <tableChoose 
      @confirm="tableChooseConfirm"
      tag-name="skillName"
      :columns="tableChooseCols"
      :inputText="'请输入技能名称或代码'" 
      :title="'选择技能'"
      :idField="idField"
      :apiMethods="'getMesHrSkillBank'"
      ref="tableChoose" 
    />
  </div>
</template>

<script>
import Vue from 'vue'
import mesPeProcess from "@/api/mesPeProcess";
import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
import tableChoose from '@/components/tableChoose/index.vue'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import _ from "lodash";
export default {
  name: "siteSkill",
  mixins:[JeecgListMixin],
  components: {
    JSuperQuery,
    tableChoose,
  },
  props: {
    options: {
      type: Array,
      default: []
    },
    siteIndex: Object,
  },
  data() {
    return {
      idField: 'skillCode',
      searchQuery: '',
      tableChooseCols: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        { 
          title: '技能编码', 
          dataIndex: 'skillCode', 
        },
        { 
          title: '技能名称', 
          dataIndex: 'skillName', 
        },
        { 
          title: '技能描述', 
          dataIndex: 'skillSprc', 
        }
      ],
      queryParamSelf: {
        skillCode: '',
        skillName: '',
        skillSprc: '',
      },
      optionsCopy: [],
      options: [],
      // 表头
      columns: [
      ],
      //列设置
      settingColumns:[],
      //列定义
      defColumns: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align: 'center',
          customRender: function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title: '技能编码',
          align: 'center',
          dataIndex: 'skillCode'
        },
        {
          title: '技能名称',
          align: 'center',
          dataIndex: 'skillName'
        },
        {
          title: '技能描述',
          align: 'center',
          dataIndex: 'skillSprc'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width:110,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      /* url: {
        list: '/mes-craft/mesPeSkill/list',
      }, */
    };
  },
  watch: {
    workstationCode(val){
      if ( val ){
        // 如果是新增，并且无数据时
        if ( this.checkOptions ){
          this.getMesPeSkillAll();
        }
      }
    },
  },
  computed: {
    checkOptions(){
      return this.options.length === 0 && this.$route.query.handle && this.$route.query.handle === 'add';
    },
    workstationCode(){
      return this.$parent.curSelectedNodes.siteAttr.workstationCode;
    },
  },
  created() {
    this.initColumns();
    // 如果是新增，并且无数据时
    if ( this.checkOptions ){
      this.getMesPeSkillAll();
    }
    this.optionsCopy = _.cloneDeep(this.options);
  },
  mounted(){
  },
  methods: {
    searchQuery(){},
    delShill(){},
    updateOption(){
      this.$store.commit('setCurNodeData', {
        ...this.siteIndex,
        options: this.options,
        field: 'skillData',
      });
      // this.$parent.curNode.data.configTree[pIndex].children[index].skillData = this.options;
      this.optionsCopy = _.cloneDeep(this.options);
      this.$refs.tableChoose.selectedRowKeys = this.options.map( el => el[this.idField] );
    },
    getMesPeSkillAll(){
      mesPeProcess.getMesPeSkillAll({
        processCode: this.$parent.curSelectedNodes.processmcode,
      }).then( res =>{
        if ( res.success ){
          this.options = res.data || [];
          this.updateOption();
        }
      })
    },
    searchQuerySelf(){
      const that = this;
      that.options = that.optionsCopy.filter( el =>{
        const skillCode = that.queryParamSelf.skillCode || el.skillCode || '';
        const skillName = that.queryParamSelf.skillName || el.skillName || '';
        const skillSprc = that.queryParamSelf.skillSprc || el.skillSprc || '';
        return (el.skillCode || '').includes(skillCode) && (el.skillName || '').includes(skillName) && (el.skillSprc ||'').includes(skillSprc);
      });
    },
    searchResetSelf(){
      this.queryParamSelf.skillCode = ''; 
      this.queryParamSelf.skillName = ''; 
      this.queryParamSelf.skillSprc = '';
      this.options = _.cloneDeep(this.optionsCopy);
    },
    tableChooseConfirm(item){
      // 并集并去重
      const arr = this.options.concat(item);
      this.options = _.uniqBy(arr, this.idField);
      this.updateOption();
    },
    getMesHrSkillBank(param){
      mesPeProcess.getMesHrSkillBank(param).then( res =>{
        if ( res.success ){
          const curData = res.data.records || [];
          this.$refs.tableChoose.tableData = curData;
          this.$refs.tableChoose.pagination.total = res.data.total;
        }
      })
    },
    handleAddShkill(){
      this.$refs.tableChoose.visible = true;
      // this.$refs.tableChoose.selectedRowKeys = this.options.map( el => el.id);
    },
    getWidth:function(){
      if(this.defColumns.length > 14){
        return {width : '500px'}
      }
    },
    batchDelSkill(){
      const that = this;
      this.$confirm({
        title: '确认删除',
        content: '是否删除选中数据?',
        onOk: function() {
          that.options = that.options.filter( el =>{
            return !that.selectedRowKeys.includes(el[this.idField] )
          });
          that.updateOption();
        }
      })
    },
    handleDeleteSkill(index){
      this.options.splice(index, 1);
      this.updateOption();
    },
    //列设置更改事件
    onColSettingsChange (checkedValues) {
      var key = this.$route.name+":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter(item => {
        if(item.key =='rowIndex'|| item.dataIndex=='action'){
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns =  cols;
    },
    initColumns(){
        this.columns =  this.defColumns;
        this.settingColumns =  this.defColumns.map(function (item) {
          return item.dataIndex;
        });
      }
  },
};
</script>
<style scoped lang='less'>
.siteSkill{
}
</style>